package com.briup.service;

import com.briup.entity.Dept;
import com.briup.entity.User;
import com.briup.util.JDBCUtil;
import org.junit.Test;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

/**
 * @author Horry
 * @Description TODO
 * @date 2022-8-31-11:45
 */
public class UserService {

	@Test
	public void test() {
		//sql语句
		String sql = "select * from t_user";
		//function接口实现类
		Function<ResultSet, List<User>> function = new Function<ResultSet, List<User>>() {
			@Override
			public List<User> apply(ResultSet resultSet) {
				List<User> list = new ArrayList<>();
				//处理rs对象
				try {
					while (resultSet.next()) {
						String username = resultSet.getString(1);
						String password = resultSet.getString(2);
						list.add(new User(username, password));
					}
				} catch (Exception e) {
					e.printStackTrace();
				}

				return list;
			}
		};
		//执行sql语句
		List<User> list = JDBCUtil.execute(sql, function);
		//输出
		list.stream().forEach(System.out::println);
	}

	@Test
	public void test2() {
		//sql语句
		String sql = "select * from t_user";
		//调用工具类封装的方法
		List<User> list = JDBCUtil.execute(sql, User.class);
		//输出
		list.stream().forEach(System.out::println);
	}

	@Test
	public void test3() {
		//sql语句
		String sql = "select * from s_dept";
		List<Dept> list = JDBCUtil.execute(sql, Dept.class);
		//输出
		list.stream().forEach(System.out::println);
		System.out.println(list.size());
	}
}
